package com.hexiang.service.impl;

import java.util.List;
import java.util.Optional;

import com.hexiang.dao.CityRepository;
import com.hexiang.domain.City;
import com.hexiang.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;

public class CityServiceImpl implements CityService {
    @Autowired
    private CityRepository cityRepository;

    @Override
    public City save(City city) {
        return cityRepository.save(city);
    }

    @Override
    public Optional<City> findCityById(Long id) {
        return cityRepository.findById(id);
    }

    @Override
    public List<City> findAllCity() {
        return cityRepository.findAll();
    }

    @Override
    public City modifyCity(City city) {
        return cityRepository.save(city);
    }

    @Override
    public Long deleteCity(Long id) {
        Optional<City> cityOptional = findCityById(id);
        boolean flag = findCityById(id).isPresent();
        if (cityOptional.isPresent()) {
            cityRepository.delete(cityOptional.get());
            return 1L;
        }
        return 0L;
    }
}
